package com.atguigu.srb.common.exception;

import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.Result;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLSyntaxErrorException;

/**
 * @author lsyandwh:
 * @create 2022-12-02 16:36
 */
//@Component
//@RestControllerAdvice//统一异常处理
public class MyExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        System.out.println("通用异常捕获");
        return Result.error().message(e.getMessage());
    }

    @ExceptionHandler(SQLSyntaxErrorException.class)
    public Result sqlSyntaxErrorException(SQLSyntaxErrorException e){
        System.out.println("sql语法结构异常捕获");
        return Result.error().message(e.getMessage());
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    public Result badSqlGrammarException(BadSqlGrammarException e){
        System.out.println("sql语法异常捕获");
        return Result.error().message(e.getMessage());
    }

    //业务异常
    @ExceptionHandler(BusinessException.class)
    public Result businessException(BusinessException e){
        System.out.println(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        return Result.error().message(e.getMessage());
    }
}
